// Andrew Gooch
// August 2022
// Repositioning Experiment


// This replication file includes:
// 1) import CVS from Qualtrics and save as DTA (done)
// 2) Drop non-consent resondents, auxiliary variables from Qualtrics, and recode variables for main analysis
// 3) Plots of 3 outcome measures by treatment group
// 4) T-test of means between treatments
// 5) Multiple regression treatment effect models confirming t-test results
// 6) Standard deviation of ideological placement by treatment assignment


//Instructions:
// To run code, change directory location first. They appear in line 49 and at the top of every plot code.
// Each step above is label (STEP ONE, STEP TWO, etc.)
// All code can be run at once.


// STEP ONE

// Original CSV is availabe in replication file
// Only change made to CVS file was deleting the redundent top row of question labels
// import CVS and turn into DTA
// Step one will be commented out after first running so that the final DTA dataset can be used with code

/*
clear all
set more off

cd "C:\Users\Andrew Gooch\Dropbox\Working papers\Repositioning\Data"

import delimited using Repositioning_data

// drop unused and auxiliary Qualtrics variables
drop responseid startdate enddate status ipaddress progress durationinseconds finished rid recordeddate recipientlastname recipientfirstname recipientemail externalreference locationlatitude locationlongitude distributionchannel userlanguage region 

// drop one pre-treatment covariate used in different manuscript
drop q5

save Repositioning_ReplicationArchive, replace */


// STEP TWO

// load DTA file 
clear all
set more off

cd "C:\Users\Andrew Gooch\Dropbox\Working papers\Repositioning\ReplicationArchive\Data"
use Repositioning_ReplicationArchive

// DROP THOSE WHO DO NOT CONSENT AND THOSE WHO FAIL ATTENTION CHECK

// drop people who did not consent
drop if q0 == "I do not agree to participate"

// drop people who failed the attention check
drop if q00 != "Cat" // 43 people failed


// STEP THREE

//This section starts by recoding variables needed to make main plots
// Main plots are treatment averages with 95 percent confidence intervals
// Need to download ciplot function before running via net install 

// VARIABLE RECODES:

// create treatment var
// Each respondent was randomized into 1 of 6 sets outcome questions (3 questions in each set)
// Each set of questions includes 1 treatment condition
// In the code below, the "if" statement captures the first outcome question from each set of question
// Respondents can only be in one of those questions, so code below captures non-missing data for those questions
// If data is not missing, that means the respondent received the treatment
gen treatment = .
replace treatment = 1 if q6_1 != .
replace treatment = 2 if q9_1 != .
replace treatment = 3 if q13_1 != .
replace treatment = 4 if q16_1 != .
replace treatment = 5 if q19_1 != .
replace treatment = 6 if q22_1 != .
label define treatment 1 "Campaign Position" 2 "Consistent" 3 "Reposition" 4 "Voters Response" 5 "Media Reponse" 6 "Activist Response" 
label values treatment treatment 
label var treatment "Treatment assignment"


//Favorability was the first outcome measure in each set of questions
//Favorability, combine each treatment question into one var
//I use rowmax to capture responses because only 1 of 6 vars below will have data
egen favorability = rowmax(q6_1 q9_1 q13_1 q16_1 q19_1 q22_1) // 129 skipped or broke off before treatment
label var favorability "Favorability 0-100"

//Competency was the second outcome measure in each set of questions
//Turn question into 0-1 scale, then combine each to make a single var
// the "_1" means treatment 1, "_2" means treatment 1, etc.
// This recoding is different than favorability because the question used 5 answer choices, and favorability used a scale

//t1
gen compt_1 = .
replace compt_1 = 1 if q7 == "Very Competent"
replace compt_1 = .75 if q7 == "Competent"
replace compt_1 = .5 if q7 == "Neither competent nor incompetent"
replace compt_1 = .25 if q7 == "Incompetent"
replace compt_1 = 0 if q7 == "Very Incompetent"
//t2
gen compt_2 = .
replace compt_2 = 1 if q10 == "Very Competent"
replace compt_2 = .75 if q10 == "Competent"
replace compt_2 = .5 if q10 == "Neither competent nor incompetent"
replace compt_2 = .25 if q10 == "Incompetent"
replace compt_2 = 0 if q10 == "Very Incompetent"
//t3
gen compt_3 = .
replace compt_3 = 1 if q14 == "Very Competent"
replace compt_3 = .75 if q14 == "Competent"
replace compt_3 = .5 if q14 == "Neither competent nor incompetent"
replace compt_3 = .25 if q14 == "Incompetent"
replace compt_3 = 0 if q14 == "Very Incompetent"
//t4
gen compt_4 = .
replace compt_4 = 1 if q17 == "Very Competent"
replace compt_4 = .75 if q17 == "Competent"
replace compt_4 = .5 if q17 == "Neither competent nor incompetent"
replace compt_4 = .25 if q17 == "Incompetent"
replace compt_4 = 0 if q17 == "Very Incompetent"
//t5
gen compt_5 = .
replace compt_5 = 1 if q20 == "Very Competent"
replace compt_5 = .75 if q20 == "Competent"
replace compt_5 = .5 if q20 == "Neither competent nor incompetent"
replace compt_5 = .25 if q20 == "Incompetent"
replace compt_5 = 0 if q20 == "Very Incompetent"
//t6
gen compt_6 = .
replace compt_6 = 1 if q23 == "Very Competent"
replace compt_6 = .75 if q23 == "Competent"
replace compt_6 = .5 if q23 == "Neither competent nor incompetent"
replace compt_6 = .25 if q23 == "Incompetent"
replace compt_6 = 0 if q23 == "Very Incompetent"

//combine outcome measures
//Again using rowmax becuase only 1 of 6 vars below will have data in them
egen comptency = rowmax(compt_1 compt_2 compt_3 compt_4 compt_5 compt_6) 
label var comptency "Competency 0-1"

//Ideology was the third question asked in the set up questions
//Same process used as competency to code ideology (question was also asked as 5 point answer choices, not a scale like favorability)
//Turn ideology question into 0-1 scale, then combine each to make a single var

//t1
gen ideo_1 = .
replace ideo_1 = 1 if q8 == "Very Liberal"
replace ideo_1 = .75 if q8 == "Liberal"
replace ideo_1 = .5 if q8 == "Moderate"
replace ideo_1 = .25 if q8 == "Conservative"
replace ideo_1 = 0 if q8 == "Very Conservative"
replace ideo_1 = 99 if q8 == "Not sure"
//t2
gen ideo_2 = .
replace ideo_2 = 1 if q11 == "Very Liberal"
replace ideo_2 = .75 if q11 == "Liberal"
replace ideo_2 = .5 if q11 == "Moderate"
replace ideo_2 = .25 if q11 == "Conservative"
replace ideo_2 = 0 if q11 == "Very Conservative"
replace ideo_2 = 99 if q11 == "Not sure"
//t3
gen ideo_3 = .
replace ideo_3 = 1 if q15 == "Very Liberal"
replace ideo_3 = .75 if q15 == "Liberal"
replace ideo_3 = .5 if q15 == "Moderate"
replace ideo_3 = .25 if q15 == "Conservative"
replace ideo_3 = 0 if q15 == "Very Conservative"
replace ideo_3 = 99 if q15 == "Not sure"
//t4
gen ideo_4 = .
replace ideo_4 = 1 if q18 == "Very Liberal"
replace ideo_4 = .75 if q18 == "Liberal"
replace ideo_4 = .5 if q18 == "Moderate"
replace ideo_4 = .25 if q18 == "Conservative"
replace ideo_4 = 0 if q18 == "Very Conservative"
replace ideo_4 = 99 if q18 == "Not sure"
//t5
gen ideo_5 = .
replace ideo_5 = 1 if q21 == "Very Liberal"
replace ideo_5 = .75 if q21 == "Liberal"
replace ideo_5 = .5 if q21 == "Moderate"
replace ideo_5 = .25 if q21 == "Conservative"
replace ideo_5 = 0 if q21 == "Very Conservative"
replace ideo_5 = 99 if q21 == "Not sure"
//t6
gen ideo_6 = .
replace ideo_6 = 1 if q24 == "Very Liberal"
replace ideo_6 = .75 if q24 == "Liberal"
replace ideo_6 = .5 if q24 == "Moderate"
replace ideo_6 = .25 if q24 == "Conservative"
replace ideo_6 = 0 if q24 == "Very Conservative"
replace ideo_6 = 99 if q24 == "Not sure"

//combined perceived candidate ideo var
egen ideology = rowmax(ideo_1 ideo_2 ideo_3 ideo_4 ideo_5 ideo_6) // 129 skipped or broke off before treatment
label var ideology "Ideology 0-1 (1=Very Lib, .5 = Mod, 0 = Very Con)"

//variable for those who cannot place ideologically
gen ideo_DK = .
replace ideo_DK = 1 if ideology == 99
replace ideo_DK = 0 if ideology != 99

//remove DK people from ideology var, recode to missing
replace ideology = . if ideology == 99


// MAIN PLOTS: FAVORABILITY, COMPETENCY, AND IDEOLOGY (AVG PLACEMENT AND CANNOT PLACE)

// FIGURE 1 favorability
// new vars for each condition for graphing purposes
// Makes favorability variables in the same way as competency and ideology
gen favor_t1 = favorability if treatment==1
label var favor_t1 "Campaign"
gen favor_t2 = favorability if treatment==2
label var favor_t2 "Consistent"
gen favor_t3 = favorability if treatment==3
label var favor_t3 "Reposition"
gen favor_t4 = favorability if treatment==4
label var favor_t4 "Voters"
gen favor_t5 = favorability if treatment==5
label var favor_t5 "Media"
gen favor_t6 = favorability if treatment==6
label var favor_t6 "Activists"
  
// PLOT
// Change cd in the plots to match your terminal
cd "C:\Users\Andrew Gooch\Dropbox\Working papers\Repositioning\Results" 
ciplot favor_t1 favor_t2 favor_t3 favor_t4 favor_t5 favor_t6 ///
, graphregion(color(white)) inclusive ylabel (35(5) 55) note(" ") l1title("Favorability in Each Conditions (0-100)") 


// FIGURE 2 competency
// new vars for each condition for graphing purposes
gen comp_t1 = comptency if treatment==1
label var comp_t1 "Campaign"
gen comp_t2 = comptency if treatment==2
label var comp_t2 "Consistent"
gen comp_t3 = comptency if treatment==3
label var comp_t3 "Reposition"
gen comp_t4 = comptency if treatment==4
label var comp_t4 "Voters"
gen comp_t5 = comptency if treatment==5
label var comp_t5 "Media"
gen comp_t6 = comptency if treatment==6
label var comp_t6 "Activists"
  
// PLOT
cd "C:\Users\Andrew Gooch\Dropbox\Working papers\Repositioning\Results" 
ciplot comp_t1 comp_t2 comp_t3 comp_t4 comp_t5 comp_t6 ///
, graphregion(color(white)) inclusive ylabel (0.3(.05) .6) note(" ") l1title("Perceived Competency in Each Condition (0-1)") 


// FIGURE 3A Ideology
// new vars for each condition for graphing purposes
gen c_ideo_1 = ideology if treatment==1
label var c_ideo_1 "Campaign"
gen c_ideo_2 = ideology if treatment==2
label var c_ideo_2 "Consistent"
gen c_ideo_3 = ideology if treatment==3
label var c_ideo_3 "Reposition"
gen c_ideo_4 = ideology if treatment==4
label var c_ideo_4 "Voters"
gen c_ideo_5 = ideology if treatment==5
label var c_ideo_5 "Media"
gen c_ideo_6 = ideology if treatment==6
label var c_ideo_6 "Activists"
  
// PLOT
cd "C:\Users\Andrew Gooch\Dropbox\Working papers\Repositioning\Results" 
ciplot c_ideo_1 c_ideo_2 c_ideo_3 c_ideo_4 c_ideo_5 c_ideo_6 ///
, graphregion(color(white)) inclusive ylabel (0.4(.05) .75) note(" ") l1title("Perceived Ideology in Each Condition (0-1)") title("Perceived Candidate Ideology")


// FIGURE 4B NOT SURE Ideology
// new vars for each condition for graphing purposes
gen ideo_DK_1 = ideo_DK if treatment==1
label var ideo_DK_1 "Campaign"
gen ideo_DK_2 = ideo_DK if treatment==2
label var ideo_DK_2 "Consistent"
gen ideo_DK_3 = ideo_DK if treatment==3
label var ideo_DK_3 "Reposition"
gen ideo_DK_4 = ideo_DK if treatment==4
label var ideo_DK_4 "Voters"
gen ideo_DK_5 = ideo_DK if treatment==5
label var ideo_DK_5 "Media"
gen ideo_DK_6 = ideo_DK if treatment==6
label var ideo_DK_6 "Activists"

  
// PLOT
cd "C:\Users\Andrew Gooch\Dropbox\Working papers\Repositioning\Results" 
ciplot ideo_DK_1 ideo_DK_2 ideo_DK_3 ideo_DK_4 ideo_DK_5 ideo_DK_6 ///
, graphregion(color(white)) inclusive ylabel (0.0(.05) .25) note(" ") l1title("Proportion Who Cannot Place Ideologically") title("Proportion Who Cannot Place the Candidate") 


// STEP FOUR

// INDEPENDENT T-TEST OF MEANS
// first make paired variables, this will be used with t-test function
// These pairs are created for all three outcome measures to assess treatment effects

// favorability

//Campaign & Consistent
gen pair12 = treatment if treatment ==1 | treatment==2
ttest favorability, by(pair12)

//Campaign & Reposition
gen pair13 = treatment if treatment ==1 | treatment==3
ttest favorability, by(pair13)

//Consistent & Reposition
gen pair23 = treatment if treatment ==2 | treatment==3
ttest favorability, by(pair23)

//Reposition & Voters
gen pair34 = treatment if treatment ==3 | treatment==4
ttest favorability, by(pair34)

//Reposition & media
gen pair35 = treatment if treatment ==3 | treatment==5
ttest favorability, by(pair35)

//Reposition & activists
gen pair36 = treatment if treatment ==3 | treatment==6
ttest favorability, by(pair36)


//ideology

//Campaign & Consistent
gen ipair12 = treatment if treatment ==1 | treatment==2
ttest ideology, by(ipair12)

//Campaign & Reposition
gen ipair13 = treatment if treatment ==1 | treatment==3
ttest ideology, by(ipair13)

//Consistent & Reposition
gen ipair23 = treatment if treatment ==2 | treatment==3
ttest ideology, by(ipair23)

//Reposition & Voters
gen ipair34 = treatment if treatment ==3 | treatment==4
ttest ideology, by(ipair34)

//Reposition & media
gen ipair35 = treatment if treatment ==3 | treatment==5
ttest ideology, by(ipair35)

//Reposition & activists
gen ipair36 = treatment if treatment ==3 | treatment==6
ttest ideology, by(ipair36)


//Cannot Place ideology

//Campaign & Consistent
gen idkpair12 = treatment if treatment ==1 | treatment==2
ttest ideo_DK, by(idkpair12)

//Campaign & Reposition
gen idkpair13 = treatment if treatment ==1 | treatment==3
ttest ideo_DK, by(idkpair13)

//Consistent & Reposition
gen idkpair23 = treatment if treatment ==2 | treatment==3
ttest ideo_DK, by(idkpair23)

//Reposition & Voters
gen idkpair34 = treatment if treatment ==3 | treatment==4
ttest ideo_DK, by(idkpair34)

//Reposition & media
gen idkpair35 = treatment if treatment ==3 | treatment==5
ttest ideo_DK, by(idkpair35)

//Reposition & activists
gen idkpair36 = treatment if treatment ==3 | treatment==6
ttest ideo_DK, by(idkpair36)


// STEP 5

// This section creates two multiple regression treatment effect models
// DVs are favorability and competency
// First recode control covariates and turn treatment vars into 0/1
// Missing values on control vars will be assigned the mean
// Then run OLS models 
// Note that excluded treatment var in regression will be repositioning (t3) to test backfire effect

//education
gen education_no_miss = education
replace education_no_miss = . if education_no_miss == -3105
summ education_no_miss // mean = 4.498
replace education = 4.498 if education == -3105
replace education = 4.498 if education == 9

//income
gen hhi_no_miss = hhi
replace hhi_no_miss = . if hhi_no_miss == -3105
summ hhi_no_miss // mean = 4.498
replace hhi = 9.489 if hhi == -3105

// race
gen white = 0
replace white = 1 if ethnicity == 1

// Trump voters
gen Trump_voter = 0
replace Trump_voter = 1 if q3 =="Donald Trump (Republican)"

//immigration is most important issue
gen immi_important = .
replace immi_important = 1 if q4 =="Immigration"
replace immi_important = 0 if q4 !="Immigration" 


//voted in 2020 election
gen turnout = .
replace turnout = 1 if q2 =="Yes, I definitely voted"
replace turnout = 0 if q2 =="I cannot recall if I voted or not" | q2 =="I usually vote, but did not in 2020" | q2 =="No, I did not vote" 

//3-point party ID
gen pid = .
replace pid = -1 if political_party == 1 | political_party == 2 | political_party == 3 | political_party ==6
replace pid = 0 if political_party == 4 | political_party == 7
replace pid = 1 if political_party == 5 | political_party == 8 | political_party == 9 | political_party==10


// Self-placed Ideology
gen self_ideology = .
replace self_ideology = -2 if q1 =="Very Liberal"
replace self_ideology = -1 if q1 =="Liberal"
replace self_ideology = 0 if q1 == "Moderate" | q1=="Not sure"
replace self_ideology = 1 if q1 =="Conservative"
replace self_ideology = 2 if q1 =="Very Conservative"


// Treatment vars recoded as dummy vars
// Therefore, coef can be interpreted as percentage point change via OLS
// Excluded regression category is repositioning (t3) to test backfire effect

// original treatment var double check
tab treatment

gen treatment_1 = 1 if treatment == 1 // consistent in office
replace treatment_1 = 0 if treatment !=1
label var treatment_1 "Campaign"

gen treatment_2 = 1 if treatment == 2 // consistent in office
replace treatment_2 = 0 if treatment !=2
label var treatment_2 "Consistent"

gen treatment_4 = 1 if treatment == 4 // voters
replace treatment_4 = 0 if treatment !=4
label var treatment_4 "Voters"

gen treatment_5 = 1 if treatment == 5 // media
replace treatment_5 = 0 if treatment !=5
label var treatment_5 "Media"

gen treatment_6 = 1 if treatment == 6 // activists
replace treatment_6 = 0 if treatment !=6
label var treatment_6 "Activists"


// Also recoding competency (DV) to match coding of favorability
//scale it 0-100 (all gets recoded except 0 ("very incompetent") becuase it is the same either way)
gen compt100 = comptency
replace compt100 = 25 if comptency == .25
replace compt100 = 50 if comptency == .5
replace compt100 = 75 if comptency == .75
replace compt100 = 100 if comptency == 1

//STEP 5

// Regression models

//FAVORABILITY
xi: reg favorability treatment_1 treatment_2 treatment_4 treatment_5 treatment_6 pid self_ideology turnout immi_important Trump_voter ///
hhi education white 
est store Favorability

//COMPETENCY 
xi: reg compt100 treatment_1 treatment_2 treatment_4 treatment_5 treatment_6 pid self_ideology turnout immi_important Trump_voter ///
hhi education white 
est store Competency

// Plot of treatment coef
// Need to download coefplot function to run this code
coefplot (Favorability, offset(-0.05) msymbol(S)) (Competency, offset(0.05) msymbol(Oh)),  ///
drop(_cons pid self_ideology turnout immi_important Trump_voter hhi education white) xline(0) ///
yline(0, lpattern(dash)) ytitle("Changes in Outcome (%) Relative to Repositioning") ///
graphregion(color(white)) vertical legend(rows(1)) legend(position(1) label(1 Favorability) label(2 Competency)) note(" ")   

//Full regression table for appendix
// Need to download outreg2 function to run this code
outreg2 [Favorability] using "Favor_Comp_Regs.xls", replace ctitle(Favorability) bracket
outreg2 [Competency] using "Favor_Comp_Regs.xls", append ctitle(Competency) bracket


// STEP 6 

//Standard deviation in Ideological placements by treatment 
summ c_ideo_1 
summ c_ideo_2
summ c_ideo_3 
summ c_ideo_4 
summ c_ideo_5 
summ c_ideo_6
